Skip to content

Conversation

@CATgwalker
Copy link
Contributor

This PR adds a cmdlet for New-ServiceNowCatalogItem which uses the 'sn_sc' API namespace to add a catalog item request to a cart and then submits the cart as an order.

Extends Get-ServiceNowAuth and Invoke-ServiceNowRestMethod with a namespace parameter to support different Namespace endpoints; backwards compatibility is maintained by defaulting to the 'now' namespace if not specified.

CATgwalker and others added 8 commits April 10, 2024 15:53
Fixes error "Id must either be a SysId 32 character alphanumeric or Number with prefix and id." and retains ID from reference table for further lookup.
Additional fixes for reference table lookup where value is not in the 32 character format expected by SNOW.
Co-authored-by: Greg Brownstein <greg@jagtechnical.com>
Add Namespace parameter to support different API endpoints; defaults to 'now'
Versioned PSD with Minor increase for new functionality and backwards-compatible changes.
@gdbarron
Copy link
Collaborator

gdbarron commented Oct 5, 2025

Awesome stuff, I'll review this week!

Add New-ServiceNowCatalog declaration to FunctionsToExport.
Copy link
Collaborator

@gdbarron gdbarron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much, looks good, just some tweaks here and there :)

Comment on lines +37 to +41
if ($Namespace -ne 'now') {
$hashOut.Uri = $($ServiceNowSession.BaseUri -split ('api'))[0] + 'api/' + $Namespace
} else {
$hashOut.Uri = $ServiceNowSession.BaseUri
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking we're better served by changing the baseuri to end with /api and just append the namespace. this will involve changes here and invoke-service nowrestmethod.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree and I tried to add the Namespace support functionality with as limited of breaking changes as I could. However, I didn't want to modify the New-ServiceNowSession function where BaseUri is originally declared into script scope variable $ServiceNowSession the due to a lack of familiarity with version specification and GraphQL use cases.

[string] $FilterString,

[parameter()]
[string] $Namespace,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default to 'now'?

Comment on lines +80 to +84
if ($namespace) {
$params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession -N $namespace
} else {
$params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ($namespace) {
$params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession -N $namespace
} else {
$params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession
}
$params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession -N $namespace


$AddItemCartResponse = Invoke-ServiceNowRestMethod @AddItemToCart

if ($AddItemCartResponse.cart_id) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why might we not get a value for cart_id that we need this 'if'?

Copy link
Contributor Author

@CATgwalker CATgwalker Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a safety check to make sure the function doesn't continue to checkout if the Invoke results in an error when adding the item to the cart. It may not be necessary but I wanted to be absolutely sure that a checkout wasn't submitted if the Invoke error didn't return a terminating error up the call stack.

Adjusted to include the CheckoutImmediately parameter.

$SubmitOrderResponse = Invoke-ServiceNowRestMethod @SubmitOrder
}
if ( $PassThru ) {
$SubmitOrderResponse
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$SubmitOrderResponse
$SubmitOrderResponse | Select-Object @{'n'='number';'e'={$_.request_number}}, request_id

consider updating the output to something like the above so it can be piped directly into the other functions, eg. Get-ServiceNowRecord

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upon further review of the API documentation, I'm not sure how this would behave when a two-step checkout process is configured.

I do not have a way to test two-step checkout at the moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: needs to be tested to validate the return behavior when multiple quantities of a catalog item or multiple catalog items are returned from the submit_order POST.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be good to have a way to control when checkout occurs in case folks want to add multiple (different) items before submitting order

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the CheckoutImmediately switch parameter as well as new function 'Submit-ServiceNowCatalogOrder' to support delayed checkout.

I feel that most use cases will want to use CheckoutImmediately as the default.

@CATgwalker
Copy link
Contributor Author

CATgwalker commented Oct 22, 2025

Thanks, I will review these recommendations after November 5th.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants